package com.yiren.backstage.judge.thread;

import android.content.Context;

import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.constant.AutoJudgeConstant;
import com.yiren.backstage.currency.entity.JudgeDetailsEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.enums.ProjectTypeEnum;
import com.yiren.backstage.currency.enums.ResultStateEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.currency.utils.MessageUtil;
import com.yiren.backstage.judge.bean.NodeBean;
import com.yiren.backstage.judge.handler.AutoJudgeHandler;
import com.yiren.backstage.judge.services.standard.IAutoJudgeThreadService;
import com.yiren.backstage.judge.services.standard.impl.AutoJudgeThreadServiceImpl;

/**
 * Time:2021/11/10 11:29
 * Author:xiaozan
 * Description:自动评判线程
 */
public class AutoJudgeThread implements Runnable {

    private AutoJudgeHandler autoJudgeHandler;

    /**根线程*/
    private AutoJudgeThread mRootTask;

    /**当前执行任务节点*/
    private NodeBean mTaskNode;

    /**当前评判所属项目*/
    private ProjectEntity projectEntity;

    /**开始评判时起始时间*/
    private long mStartTime;

    /**实时车辆状态对象*/
    private CarStateBean realCarState;

    /**开始车辆状态*/
    private CarStateBean mStartCarState;

    /**
     * 标准项目：项目评判总节点数
     * 全程评判：单个评判的总节点数
     */
    private int sumNodeCount;

    /**
     * 标准项目:项目已执行的节点数.
     * 全程评判：单个评判已执行的节点数
     */
    private int mExecutedNodeCount;

    /**任务完成状态*/
    private TaskStateEnum mTaskState = TaskStateEnum.IN_PROCESS;

    private IAutoJudgeThreadService iAutoJudgeThreadService;

    public AutoJudgeThread(Context context,ProjectEntity projectEntity, AutoJudgeHandler autoJudgeHandler) {
        this.autoJudgeHandler = autoJudgeHandler;
        this.projectEntity = projectEntity;
        iAutoJudgeThreadService = new AutoJudgeThreadServiceImpl(context,autoJudgeHandler);
        initParameters();
    }

    /**
     * 初始化参数
     */
    public void initParameters() {
        realCarState = CarStateBean.getInstance();
        //设置开始时间
        mStartTime = System.currentTimeMillis();
        //设置初始化车辆状态
        mStartCarState = new CarStateBean();
        mStartCarState.setTotalMileage(realCarState.getTotalMileage());
        mStartCarState.setNsbsAngleDiff(realCarState.getNsbsAngleDiff());
    }

    @Override
    public void run() {
        //关闭所有线程（结束考试、退出考试）
        if(autoJudgeHandler.getTaskStateEnum() == TaskStateEnum.FORCT_CLOSE){
            return;
        }
        //项目根线程节点为null 不累计执行次数（根节点是手动创建的）
        if(null == mTaskNode || null == mTaskNode.getEntity()){
            return;
        }
        //任务节点全部已执行完成、终结项目和结束考试时，将项目状态变成已完成状态。关闭根节点下的所有线程
        if (null != mRootTask && mRootTask.mTaskState == TaskStateEnum.FINISH) {
            return;
        }

        //获取执行结果
        ResultStateEnum resultStateEnum = iAutoJudgeThreadService.executeResult(this);
        if(resultStateEnum != ResultStateEnum.EXECUTING){
            //结果处理
            iAutoJudgeThreadService.runExecuteResultWith(this,resultStateEnum);

            //判断当前已执行节点数是否大于等于总节点数
            if (mRootTask.mExecutedNodeCount >= mRootTask.sumNodeCount) {
                //判断项目类型，全程评判重新执行，标准项目为已完成
                if(projectEntity.getProjectType().equals(ProjectTypeEnum.SUBJECT_THREE_DAYTIME.toString())){
                    mRootTask.setmTaskState(TaskStateEnum.FINISH);
                    //播报项目结束语音
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.PLAY_PROJECT_END_VOICE, projectEntity));
                }else{
                    //重新执行根评判
                    autoJudgeHandler.sendMessage(MessageUtil.newMessage(AutoJudgeConstant.AGAIN_EXECUTE_JUDGE, this));
                }
            }
            return;
        }
        //延迟执行
        autoJudgeHandler.postDelayed(this, 10);
    }

    public void setmRootTask(AutoJudgeThread mRootTask) { this.mRootTask = mRootTask; }

    public void setmTaskNode(NodeBean mTaskNode) {this.mTaskNode = mTaskNode; }

    public void setmTaskState(TaskStateEnum mTaskState) {
        this.mTaskState = mTaskState;
    }

    public void setSumNodeCount(int sumNodeCount) { this.sumNodeCount = sumNodeCount; }

    public int getSumNodeCount() { return sumNodeCount; }

    public NodeBean getmTaskNode() {
        return mTaskNode;
    }

    public AutoJudgeThread getmRootTask() {
        return mRootTask;
    }

    public ProjectEntity getProjectEntity() {
        return projectEntity;
    }

    public int getmExecutedNodeCount() { return mExecutedNodeCount; }

    public void setmExecutedNodeCount(int mExecutedNodeCount) { this.mExecutedNodeCount = mExecutedNodeCount; }

    public long getmStartTime() {
        return mStartTime;
    }

    public void setmStartTime(long mStartTime) {
        this.mStartTime = mStartTime;
    }

    public CarStateBean getmStartCarState() {
        return mStartCarState;
    }

    public void setmStartCarState(CarStateBean mStartCarState) {
        this.mStartCarState = mStartCarState;
    }
}
